//
//  SelectionViewController.m
//  Taste
//
//  Created by 徐昊 on 16/9/2.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "SelectionViewController.h"
#import "SelectionCollectionViewCell.h"
#import "HXMacro.h"
#import "ZWYNetTool.h"
#import "SelectionModel.h"

#define kSelectionCollectionViewCellName @"SelectionCollectionViewCellNameReuse"
@interface SelectionViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) SelectionModel *model;
@property (weak, nonatomic) IBOutlet UIButton *recommend;
@property (weak, nonatomic) IBOutlet UIButton *consume;
@property (weak, nonatomic) IBOutlet UIButton *distance;
@property (weak, nonatomic) IBOutlet UIButton *recommendIndex;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leading;
@property (nonatomic, strong) NSMutableArray *arrayOfButton;


@end

@implementation SelectionViewController

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"精选餐厅";
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Local"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(location:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(search:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _layout.itemSize = CGSizeMake(SCREEN_WIDTH, _collectionView.bounds.size.height);
    _layout.minimumInteritemSpacing = 0;
    _layout.minimumLineSpacing = 0;
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView.scrollEnabled = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.bounces = NO;
    _collectionView.pagingEnabled = YES;
    _collectionView.dataSource = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"SelectionCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kSelectionCollectionViewCellName];
    _arrayOfButton = @[_recommend, _consume, _distance, _recommendIndex].mutableCopy;
    for (int i = 0; i < _arrayOfButton.count; i++) {
        UIButton *btn = _arrayOfButton[i];
        btn.tag = i;
        [btn addTarget:self action:@selector(myClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    _recommend.selected = YES;
}
- (void)myClick:(UIButton *)sender {
    for (UIButton *btn in _arrayOfButton) {
        btn.selected = NO;
    }
    sender.selected = YES;
    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * sender.tag, 0);
    _leading.constant = _recommend.bounds.size.width * sender.tag;
    [UIView animateWithDuration:0.2 animations:^{
        [self.view layoutIfNeeded];
    }];
}
- (void)location:(UIBarButtonItem *)button {
    
}
- (void)search:(UIBarButtonItem *)button {
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SelectionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSelectionCollectionViewCellName forIndexPath:indexPath];
    cell.num = indexPath.row;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
